if __name__ == '__main__':
    import os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'JiXuShopSystem.settings')
    import django
    django.setup()


    from goods.models import *
    import json
    import random
    import requests
    import re
    from pypinyin import pinyin, Style

    class Spider():
        def __init__(self):

            self.headers = {
                "accept": "application/json, text/javascript, */*; q=0.01",
                "accept-language": "zh-CN,zh;q=0.9",
                "authorization": "OAuth api_sign=d463028caef4688f248d1d05cac958d0153afe12",
                "cache-control": "no-cache",
                "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
                "origin": "https://detail.vip.com",
                "pragma": "no-cache",
                "priority": "u=1, i",
                "referer": "https://detail.vip.com/",
                "sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"Windows\"",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-site",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
                "x-requested-with": "XMLHttpRequest"
            }
            self.cookies = {
                "vip_cps_cid": "1703946155095_f8aebf721aa4d69f55487762e3ca4c21",
                "PAPVisitorId": "58460c7a22e31f6b4acb2a1ed741f921",
                "vip_new_old_user": "1",
                "mars_cid": "1703946163504_4eebec221de3364e0da3bbe4a2182454",
                "mars_pid": "0",
                "vip_cps_cuid": "CU1715695090775e67fbb574e7ca3b54",
                "vip_city_name": "%E5%B9%BF%E5%B7%9E%E5%B8%82",
                "VipUINFO": "luc%3Aa%7Csuc%3Aa%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A1%7Cp4%3A0%7Cp5%3A0%7Cul%3A3105",
                "vip_address": "%257B%2522pname%2522%253A%2522%255Cu5b89%255Cu5fbd%255Cu7701%2522%252C%2522pid%2522%253A%2522103104%2522%252C%2522cname%2522%253A%2522%255Cu5e7f%255Cu5dde%255Cu5e02%2522%252C%2522cid%2522%253A%2522103104105%2522%257D",
                "vip_province": "103104",
                "vip_province_name": "%E5%AE%89%E5%BE%BD%E7%9C%81",
                "vip_city_code": "103104105",
                "vip_wh": "VIP_HZ",
                "vip_ipver": "31",
                "user_class": "a",
                "visit_id": "DBEBA17A04F9893A4CBF22BF2C37ADB4",
                "mars_sid": "f9ea9bcbdad7fcd940397690e20183a7",
                "VIP_QR_FIRST": "1",
                "vip_tracker_source_from": "",
                "vipshop_passport_src": "https%3A%2F%2Fdetail.vip.com%2Fdetail-1711573035-6920320379434192651.html",
                "pg_session_no": "11",
                "VipDFT": "-1",
                "vip_access_times": "%7B%22list%22%3A3%2C%22detail%22%3A0%7D"
            }
            self.url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/detail/main/v6"


        # 获取品牌表的数据
        def brand(self,response):
            # 品牌
            brandStoreInfo = response['data']['brandStoreInfo']['brandStoreName']
            # Logo
            brandStoreLogo = response['data']['brandStoreInfo']['brandStoreLogo']
            # 首字母
            if brandStoreInfo:
                first_char_pinyin_list = pinyin(brandStoreInfo[0], style=Style.FIRST_LETTER, strict=False)
                # 取第一个拼音的第一个字母（转换为大写）
                first_letter =  first_char_pinyin_list[0][0].upper()
            else:
                first_letter = None
            brand_data = {
                'brandStoreInfo':brandStoreInfo,
                'brandStoreLogo':brandStoreLogo,
                'first_letter':first_letter,
            }
            return brand_data

        # 获取商品SPU数据
        def spu(self,response):
            # spu
            spu_name = response['data']['base']['title']
            print(spu_name)
            spu_sales = 0
            spu_comments = 0

            images_detls = response['data']['images']['groups'].values()
            detail = ''
            for detil in images_detls:
                # 介绍
                detailImages = detil['detailImages']
                for imageUrls in detailImages[1:]:
                    imageUrl = imageUrls['imageUrl']
                    detail += f'<p><img src="{imageUrl}"></p>'
                break


            url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/detail/vendorqa/v1"
            params = {
                "callback": "getVendorQaCb",
                "app_name": "shop_pc",
                "app_version": "4.0",
                "warehouse": "VIP_HZ",
                "fdc_area_id": "103104105",
                "client": "pc",
                "mobile_platform": "1",
                "province_id": "103104",
                "api_key": "70f71280d5d547b2a7bb370a529aeea1",
                "user_id": "",
                "mars_cid": "1703946163504_4eebec221de3364e0da3bbe4a2182454",
                "wap_consumer": "a",
                "productId": "6920549355391848472"
            }
            response_pack = requests.get(url, headers=self.headers, cookies=self.cookies, params=params).text.replace('getVendorQaCb(','').replace(')','')
            response_pack_dict = json.loads(response_pack)
            decs_pack = response_pack_dict['data']['list']



            # 包装清单
            decs_data = ''
            for decs in decs_pack:
                decs_data += f"""
                <dl class="q-tit-item">
                        <dt class="qti-title">
                            <i class="qti-question"></i>
                            <p class="qti-txt" style="color: red">{decs['question']}</p>
                        </dt>
                        <dd class="qti-content">
                            <i class="qti-answer"></i>
                            <p class="qti-txt">
                                {decs['answer']}
                            </p>
                        </dd>
                    </dl>
                """


            # 售后服务
            tsaleService_data = ''
            tsaleServiceListitle = response['data']['saleServiceList']
            for tsaleService in tsaleServiceListitle:
                tsaleService_data += f"""
                    <dl class="q-tit-item">
                            <dt class="qti-title">
                                <i class="qti-question"></i>
                                <p class="qti-txt" style="color: red">{tsaleService['name']}</p>
                            </dt>
                            <dd class="qti-content">
                                <i class="qti-answer"></i>
                                <p class="qti-txt">
                                    {tsaleService['value']}
                                </p>
                            </dd>
                        </dl>
                    """

            spu_data = {
                'name' : spu_name,
                'sales' : spu_sales,
                'comments' : spu_comments,
                'desc_detail' : detail,
                'desc_pack' : decs_data,
                'desc_service' : tsaleService_data

            }
            return spu_data

        # 获取商品SKU数据
        def sku(self,response):
            title = response['data']['base']['title']

            saleProps_list = []
            products = response['data']['products'].values()

            for index , product in enumerate(products):
                # 标题
                longTitle = product['merchandiseSn']
                # 名称
                merchandiseSn = product['merchandiseSn']
                # 市场价
                saleMarketPrice = product['priceView']['salePrice']['saleMarketPrice']
                if not saleMarketPrice:
                    saleMarketPrice = 0
                # 售价
                salePrice = product['priceView']['salePrice']['salePrice']
                if not salePrice:
                    salePrice = 0
                # 折扣
                try:
                    cost_price = int(saleMarketPrice) - int(salePrice)
                except:
                    cost_price = 0
                if not cost_price:
                    cost_price = 0
                # 库存
                stock = 1000
                # 销量
                sales = random.randint(1, 800)
                # 评价
                comments = 0
                # 是否上架
                if index==0:
                    is_launched = True
                else:
                    is_launched = False
                # 默认图片
                default_image = product['smallImage']
                # 商品ID
                productId = product['productId']
                saleProps_dict = {
                    'name': merchandiseSn,
                    'price': salePrice,
                    'caption': title,
                    'cost_price': cost_price,
                    'market_price': saleMarketPrice,
                    'stock': stock,
                    'sales': sales,
                    'comments': comments,
                    'is_launched': is_launched,
                    'default_image': default_image,
                    'productId': productId
                }

                saleProps_list.append(saleProps_dict)
            return saleProps_list

        # 获取商品图片
        def sku_image(self,response):
            images_detls = response['data']['images']['groups'].values()
            SKUImage_detail = []
            # 商品详情图
            for detil in images_detls:
                image_lsit = detil['previewImages']
                for image_urls in image_lsit:
                    image_url = image_urls['imageUrl'].replace('http://h2.appsimg.com/a.appsimg.com/upload/merchandise/','').replace('.jpg','')
                    SKUImage_detail.append(image_url)
                break

            return SKUImage_detail

        # 获取商品规格数据
        def spu_pecification(self,response):
            # 规格
            saleProps_list = []
            saleProps = response['data']['saleProps']
            for saleProp in saleProps:
                name = saleProp['name']
                saleProps_list.append(name)
            return saleProps_list

        # 获取商品具体规格数据
        def spu_optiuons(self, response):
            saleProps = response['data']['saleProps']
            sub_dict = {}
            type_list = []
            for index, saleProp in enumerate(saleProps):
                type_name = saleProp['name']
                type_list.append(type_name)
                data_list = []
                for sub_saleProp in saleProp.get('values'):
                    detail_name = sub_saleProp['name']
                    # 如果第一级素具不存在productId则productId按第二级数据取
                    if sub_saleProp.get('productId') is None:
                        productId = ''
                    else:
                        productId = sub_saleProp['productId']

                    data_dict = {
                        'productId': productId,
                        'detail_name': detail_name,
                    }
                    data_list.append(data_dict)

                if type_name not in sub_dict:
                    sub_dict[type_name] = data_list

            return sub_dict


        # 获得所有商品规格信息
        def good_type(self, response):
            tatil_dict = {}
            saleProps = response['data']['saleProps']
            for saleProp in saleProps:
                pid = saleProp['pid']
                name = saleProp['name']
                type = saleProp['type']
                sale_dict = {
                    'name': name,
                    'pid': pid,
                    'type': type,
                }
                value_list = []
                for value in saleProp['values']:
                    name = value['name']
                    vid = value['vid']
                    value_dict = {
                        'name': name,
                        'vid': vid,
                    }
                    sale_dict[vid] = name
                    value_list.append(value_dict)
                sale_dict['values'] = value_list
                tatil_dict[pid] = sale_dict
            return tatil_dict

        # 拿到所有的商品规格
        def get_type(self, response):

            # pprint.pprint(tatil_dict)

            skus = response['data']['skus']
            sku_keys = skus.keys()
            sku_values = skus.values()
            productId_dict = {}
            for key, value in zip(sku_keys, sku_values):
                productId = value['productId']
                ids = key.split(';')
                id_list = []
                for id in ids:
                    pid = id.split(':')[0]
                    vid = id.split(':')[1]

                    id_dict = {
                        'pid': pid,
                        'vid': vid,
                    }
                    id_list.append(id_dict)
                productId_dict[productId] = id_list

            return productId_dict


        # 循环调用
        def main(self):
            for page in range(0, 2400, 120):


                headers = {
                    "authority": "mapi.vip.com",
                    "accept": "*/*",
                    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                    "cache-control": "no-cache",
                    "pragma": "no-cache",
                    "referer": "https://category.vip.com/",
                    "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Microsoft Edge\";v=\"122\"",
                    "sec-ch-ua-mobile": "?0",
                    "sec-ch-ua-platform": "\"Windows\"",
                    "sec-fetch-dest": "script",
                    "sec-fetch-mode": "no-cors",
                    "sec-fetch-site": "same-site",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
                }
                cookies = {
                    "VipDFT": "1",
                    "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
                    "vip_first_visitor": "1",
                    "vip_address": "%257B%2522pid%2522%253A%2522103104%2522%252C%2522cid%2522%253A%2522103104105%2522%252C%2522pname%2522%253A%2522%255Cu5b89%255Cu5fbd%255Cu7701%2522%252C%2522cname%2522%253A%2522%255Cu9a6c%255Cu978d%255Cu5c71%255Cu5e02%2522%257D",
                    "vip_province": "103104",
                    "vip_province_name": "%E5%AE%89%E5%BE%BD%E7%9C%81",
                    "vip_city_name": "%E9%A9%AC%E9%9E%8D%E5%B1%B1%E5%B8%82",
                    "vip_city_code": "103104105",
                    "vip_wh": "VIP_HZ",
                    "vip_ipver": "31",
                    "user_class": "a",
                    "VipUINFO": "luc%3Aa%7Csuc%3Aa%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A0%7Cp4%3A0%7Cp5%3A1%7Cul%3A3105",
                    "vpc_uinfo": "fr1352%3A0%2Cfr674%3AD1%2Cfr766%3A0%2Cfr896%3A0%2Cfr392%3A310505%2Cfr398%3A0%2Cfr408%3A0%2Cfr251%3AA%2Cfr1195%3A0%2Cfr848%3A0%2Cfr1196%3A0%2Cfr902%3A0%2Cfr901%3A0%2Cfr980%3A0%2Cfr1570%3A0%2Cfr713%3A0%2Cfr1575%3A0%2Cfr1051%3A0%2Cfr259%3AS0-4%2Cfr1655%3A0%2Cfr884%3A0%2Cfr863%3A0%2Cfr1527%3A0%2Cfr344%3A0%2Cfr249%3AA1%2Cfr328%3A3105%2Cfr1544%3A0%2Cfr1543%3A0%2Cfr1521%3A0",
                    "mst_area_code": "104104",
                    "mars_sid": "7a82bf0c7f450720de72e1f84101eb3d",
                    "visit_id": "DEBBACD09723B4F080630FD1B99C700F",
                    "VIP_QR_FIRST": "1",
                    "mars_pid": "35",
                    "vip_access_times": "%7B%22detail%22%3A4%2C%22list%22%3A5%7D",
                    "pg_session_no": "11",
                    "vip_tracker_source_from": ""
                }
                url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank"
                params = {
                    "callback": "getMerchandiseIds",
                    "app_name": "shop_pc",
                    "app_version": "4.0",
                    "warehouse": "VIP_HZ",
                    "fdc_area_id": "103104105",
                    "client": "pc",
                    "mobile_platform": "1",
                    "province_id": "103104",
                    "api_key": "70f71280d5d547b2a7bb370a529aeea1",
                    "user_id": "",
                    "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
                    "wap_consumer": "a",
                    "standby_id": "nature",
                    "keyword": "手机",
                    "lv3CatIds": "",
                    "lv2CatIds": "",
                    "lv1CatIds": "",
                    "brandStoreSns": "",
                    "props": "",
                    "priceMin": "",
                    "priceMax": "",
                    "vipService": "",
                    "sort": "0",
                    "pageOffset": "0",
                    "channelId": "1",
                    "gPlatform": "PC",
                    "batchSize": "120",
                    "_": "1716219865667"
                }
                response = requests.get(url, headers=headers, cookies=cookies, params=params).text

                # 获得产品编码
                pid_code = re.findall('{"pid":"(.*?)"}', response)
                print(pid_code)

                for pid in pid_code:
                    self.spider_data(pid)


        # 主函数
        def spider_data(self,pid):
            self.data = {
                "app_name": "shop_pc",
                "app_version": "4.0",
                "warehouse": "VIP_HZ",
                "fdc_area_id": "103104105",
                "client": "pc",
                "mobile_platform": "1",
                "province_id": "103104",
                "api_key": "70f71280d5d547b2a7bb370a529aeea1",
                "user_id": "",
                "mars_cid": "1703946163504_4eebec221de3364e0da3bbe4a2182454",
                "wap_consumer": "a",
                "scene": "detail",
                "productId": f"{pid}",
                "opts": "priceView:13;quotaInfo:1;restrictTips:1;panelView:3;foreShowActive:1;invisible:1;floatingView:1;announcement:1;svipView:2;showSingleColor:1;svipPriceMode:1;promotionTips:6;foldTips:3;formula:2;extraDetailImages:1;shortVideo:1;countryFlagStyle:1;saleServiceList:1;storeInfo:2;brandCountry:1;freightTips:3;priceBannerView:1;bannerTagsView:1;buyMoreFormula:1;mergeGiftTips:0;kf:1;priceIcon:1;tuv:3;promotionTags:7;mergeGiftTips:3;topDetailImage:2;deliveryInfo:1;relatedProdSpu:1"
            }
            response = requests.post(self.url, headers=self.headers, cookies=self.cookies, data=self.data).json()

            brand_data = self.brand(response)


            brand_object = Brand.objects.create(
                name=brand_data['brandStoreInfo'],
                logo=brand_data['brandStoreLogo'],
                first_letter=brand_data['first_letter']
            )

            good_category1 = GoodsCategory.objects.get(id=1)
            good_category2 = GoodsCategory.objects.get(id=38)
            good_category3 = GoodsCategory.objects.get(id=115)

            spu_data = self.spu(response)


            # 有哪些商品  (Apple MacBook Pro 笔记本)
            spu_object = SPU.objects.create(
                name=spu_data['name'],
                sales=spu_data['sales'],
                comments=spu_data['comments'],
                desc_detail=spu_data['desc_detail'],
                desc_pack=spu_data['desc_pack'],
                desc_service=spu_data['desc_service'],
                brand=brand_object,
                category1=good_category1,
                category2=good_category2,
                category3=good_category3,
            )


            tpyes = self.spu_pecification(response)
            for type in tpyes:
                spu_specification = SPUSpecification.objects.create(
                    name=type,
                    spu=spu_object

                )
                spu_optiuons_dict = self.spu_optiuons(response)
                detail_list = spu_optiuons_dict[type]
                for detail in detail_list:
                    detail_name = detail['detail_name']
                    SpecificationOption_object = SpecificationOption.objects.create(
                        value=detail_name,
                        spec=spu_specification
                    )



            # 商品的对应型号  (Apple MacBook Pro 13.3英寸笔记本 银色)  (Apple MacBook Pro 13.3英寸笔记本 深灰色)
            sku_data_list = self.sku(response)

            for index,sku_data in enumerate(sku_data_list):
                sku_object = SKU.objects.create(
                    name=sku_data['name'],
                    price=sku_data['price'],
                    caption=sku_data['caption'],
                    cost_price=sku_data['cost_price'],
                    market_price=sku_data['market_price'],
                    stock=sku_data['stock'],
                    sales=sku_data['sales'],
                    comments=sku_data['comments'],
                    is_launched=sku_data['is_launched'],
                    default_image=sku_data['default_image'],
                    category=good_category3,
                    spu=spu_object
                )


                # 对应商品的图片
                sku_image_list = self.sku_image(response)
                for sku_image_data in sku_image_list:
                    SKUImage.objects.create(
                        image=sku_image_data,
                        sku= sku_object
                    )



                # 获取该商品对应规格
                tatil_dict = self.good_type(response)
                productId_dict = self.get_type(response)
                productId = sku_data['productId']
                params = productId_dict[productId]
                for param in params:
                    pid = param['pid']
                    vid = param['vid']

                    param_dict = {
                        'name': tatil_dict[pid]['name'],
                        'type': tatil_dict[pid][vid]
                    }
                    spu_id = sku_object.spu_id
                    SPUSpecification_object = SPUSpecification.objects.get(spu_id=spu_id, name=param_dict['name'])
                    spec_id = SPUSpecification_object.id
                    option_object = SpecificationOption.objects.get(spec_id=spec_id , value=param_dict['type'])
                    SKUSpecification.objects.create(
                        option=option_object,
                        sku=sku_object,
                        spec=SPUSpecification_object
                    )


    spider = Spider()
    spider.main()